<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model.trim="listQuery.fno" clearable class="filter-item" style="width: 200px;" placeholder="请输入客户编号" type="number"/><!-- oninput="value=value.replace(/[^\d]/g,'')"-->
      <el-input v-model.trim="listQuery.fname" clearable class="filter-item" style="width: 200px;" placeholder="请输入客户名称"/>
      <!-- v-permission="['sys:tenant:read']" -->
      <el-button class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <!-- v-permission="['sys:tenant:create']" -->
      <el-button class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
      <!--<el-table-column align="center" label="假期编号" prop="fholidayid"/>-->
      <el-table-column align="center" label="客户编号" prop="fno"/>
      <el-table-column align="center" label="客户名称" prop="fname"/>
      <el-table-column align="center" label="联系人信息" prop="flinkMan"/>
      <el-table-column align="center" label="客户地址" prop="faddress"/>
      <el-table-column align="center" label="服务Api地址" prop="fapiUrl"/>
      <el-table-column :formatter="dateFormat" align="center" label="授权开始日期" prop="fbeginDate"/>
      <el-table-column :formatter="dateFormat" align="center" label="授权结束日期" prop="fendDate"/>
      <el-table-column align="center" label="状态" prop="fstatus">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.fstatus | statusFilter }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" width="140" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <!-- v-permission="['sys:tenant:update']" -->
          <el-button :style="{background:buttonColor.edit,color:buttonColor.color}" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <!-- v-permission="['sys:tenant:delete']" -->
          <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 500px; margin-left: 20px;">
        <el-row>
          <el-col :span="10">
            <el-form-item label="客户编号" label-width="110px" prop="fno" style="width: 250px">
              <el-input v-model="dataForm.fno" type="number"/><!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="客户名称" label-width="110px" prop="fname" style="width: 250px">
              <el-input v-model="dataForm.fname"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="联系人信息" label-width="110px" prop="flinkMan" style="width: 250px">
              <el-input v-model="dataForm.flinkMan"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="客户地址" label-width="110px" prop="faddress" style="width: 250px">
              <el-input v-model="dataForm.faddress"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="服务Api地址" label-width="110px" prop="fapiUrl" style="width: 250px">
              <el-input v-model="dataForm.fapiUrl"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item class="el-form-item-small" label="状态" prop="fstatus" style="width: 250px">
              <el-select v-model="dataForm.fstatus" class="filter-item" placeholder="请选择状态">
                <el-option v-for="(key, value) in statusMap" :key="key" :label="key" :value="value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="授权开始日期" label-width="110px" prop="fbeginDate">
              <el-date-picker v-model="dataForm.fbeginDate" type="date" style="width: 140px" clearable class="filter-item" placeholder="选择授权开始日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="授权结束日期" label-width="110px" prop="fendDate">
              <el-date-picker v-model="dataForm.fendDate" type="date" style="width: 140px" clearable class="filter-item" placeholder="选择授权结束日期"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listAppVersion, createAppVersion, updateAppVersion, deleteAppVersion } from '@/api/tenant'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import {buttonColor} from '@/filters/index'
const siteTypeMap = {
  '01': '空气',
  '02': 'SMART',
  '03': '维护',
  '04': '轶伦APP'
}

const statusMap = {
  0: '禁用',
  1: '启用'
}

export default {
  name: 'Tenant',
  components: { Pagination },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type]
    },
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fno: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      siteTypeMap,
      statusMap,
      dataForm: {
        id: undefined,
        fno: undefined,
        fname: undefined,
        flinkMan: undefined,
        faddress: undefined,
        fapiUrl: undefined,
        fbeginDate: undefined,
        fendDate: undefined,
        fstatus: undefined,
        fupddate: undefined,
        foperator: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fno: [
          { required: true, message: '客户编号不能为空', trigger: 'blur' }
        ],
        fname: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        // flinkMan: [
        //   { required: true, message: '联系人信息不能为空', trigger: 'blur' }
        // ],
        // faddress: [
        //   { required: true, message: '客户地址不能为空', trigger: 'blur' }
        // ],
        // fapiUrl: [
        //   { required: true, message: '服务Api地址不能为空', trigger: 'blur' }
        // ],
        fbeginDate: [
          { required: true, message: '授权开始日期不能为空', trigger: 'blur' }
        ],
        fendDate: [
          { required: true, message: '授权结束日期不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listAppVersion(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fno: undefined,
        fname: undefined,
        flinkMan: undefined,
        faddress: undefined,
        fapiUrl: undefined,
        fbeginDate: undefined,
        fendDate: undefined,
        fstatus: undefined,
        fupddate: undefined,
        foperator: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.fstatus = '1'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createAppVersion(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加客户资料成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dataForm.fstatus = row.fstatus.toString()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateAppVersion(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新客户资料资料成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.msg
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该客户资料资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAppVersion(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除客户资料资料成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
<style>
  /*处理input type = number的上下箭头*/
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type="number"]{
    -moz-appearance: textfield;
  }
</style>
